package com.zhouwr.common.metadata;

import com.alibaba.fastjson.JSON;
import com.zhouwr.common.enums.ModelDataType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;


/**
 * @program: forallcn-iotps
 * @description: 单精度浮点
 * @author: zhouwr
 * @create: 2020-11-19 16:20
 * @version：1.0
 **/
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class FloatData extends Metadata<Float> {
    private String unit;
    private Float maxValue;
    private Float minValue;
    private Float accuracy;
    private Float defaultValue = 0f;
    private ModelDataType type = ModelDataType.FLOAT;


    @Override
    public String getUnitName() {
        if (StringUtils.isBlank(unit)) {
            return "";
        }
        return unit.substring(0, unit.indexOf("["));
    }

    @Override
    public String getUnitSymbol() {
        if (StringUtils.isBlank(unit)) {
            return "";
        }
        return unit.substring(unit.indexOf("[") + 1, unit.indexOf("]"));
    }

    @Override
    public Float getDefaultValue() {
        return this.defaultValue;
    }


    public static Metadata<?> form(String metadata) {
        return JSON.toJavaObject(JSON.parseObject(metadata), FloatData.class);
    }
}
